<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <page-tools>
          <!-- 插入到left插槽位置 -->
          <template #left>
            <span>总人数：{{ total }}</span>
          </template>
          <!-- 插入到right插槽位置 -->
          <template #right>
            <el-button type="primary" size="small" @click="$router.push('/uploadExcel')">导入</el-button>
            <el-button type="warning" size="small" @click="exportExcel">导出</el-button>
            <el-button type="success" size="small" @click="showDialog = true">新增员工</el-button>
          </template>
        </page-tools>
      </el-card>
      <el-card style="margin-top: 10px;">
        <el-table border :data="list" :default-sort="{prop: 'workNumber'}">
          <el-table-column label="序号" type="index" width="80" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" width="110">
            <template #default="scope">
              <!-- <img :src="scope.row.staffPhoto" alt="" width="80"> -->
              <el-image :src="scope.row.staffPhoto" class="avatar" lazy style="width: 100px; height: 100px">
                <!-- 具名插槽新语法  图片加载出错的时候自动显示 -->
                <template #error>
                  <!-- <i class="el-icon-picture-outline" /> -->
                  <img src="@/assets/common/bigUserHeader.png" width="80">
                </template>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="scope">
              <!-- 插值表达式支持函数执行 并且渲染的是函数的return值 -->
              {{ formatEmployment(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry">
            <template #default="scope">
              {{ _formatTime(scope.row.timeOfEntry) }}
            </template>
          </el-table-column>

          <el-table-column label="操作" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small" @click="goDetail(row.id)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="editRole(row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="hDelEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            :total="total"
            :current-page="params.page"
            :page-size="params.pageSize"
            layout="prev, pager, next"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
      <add-employee :show-dialog="showDialog" @close-dialog="closeDialog" />
      <!-- 分配权限 -->
      <assign-role :show-role-dialog="showRoleDialog" :cur-id="curId" @close-dialog="closeAssignDialog" />
    </div>
  </div>
</template>
<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import { getExportData } from '@/utils/excelData'
import AssignRole from './components/assign-role'
import dayjs from 'dayjs'
export default {
  name: 'Employees',
  components: {
    AssignRole
  },
  data() {
    return {
      list: [],
      params: {
        page: 1, // 当前页
        pageSize: 10 // 每页条数
      },
      total: 0, // 总数
      loading: false,
      showDialog: false,
      showRoleDialog: false,
      curId: ''
    }
  },
  mounted() {
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      this.loading = true
      const data = await getEmployeeList(this.params)
      const { total, rows } = data
      this.total = total
      this.list = rows
      this.loading = false
    },
    handleCurrentChange(newPage) {
      this.params.page = newPage
      this.getEmployeeList()
    },
    // 格式化聘用形式函数
    formatEmployment(type) {
      // 根据源数据经过处理 把处理之后的数据返回
      // 定义一个映射结构
      // {1::'正式', 0:'非正式'}
      const map = {
        1: '正式',
        2: '非正式'
      }
      return map[type]
    },
    // 入职时间格式化
    _formatTime(value, str = 'YYYY-MM-DD') {
      return dayjs(value).format(str)
    },
    // 删除
    hDelEmployee(id) {
      this.$confirm('你确认要删除么?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployee(id)
        this.getEmployeeList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 新增
    closeDialog() {
      this.showDialog = false
    },
    // 导出
    exportExcel() {
      import('@/vendor/Export2Excel').then(async excel => {
        // 1.准备表头中的中英文对应关系
        const headerRelation = {
          '手机号': 'mobile',
          '姓名': 'username',
          '入职日期': 'timeOfEntry',
          '工号': 'workNumber',
          '转正日期': 'correctionTime',
          '部门': 'departmentName'
        }

        // 2.调用接口准备表格数据 (从数组库拉取当前页的最新的数据完成导出)
        const res = await getEmployeeList(this.params)
        // 3.调用处理方法进行数据处理
        const { data, header } = getExportData(res.rows, headerRelation)
        excel.export_json_to_excel({
          header: header, // 表头数据
          data: data, // 表格数据 (二维数组)
          filename: '员工列表',
          autoWidth: true,
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    // 查看
    goDetail(id) {
      this.$router.push({
        path: '/employees/detail',
        query: {
          id
        }
      })
    },
    // 分配角色
    editRole(id) {
      this.showRoleDialog = true
      this.curId = id
    },
    // 子传父关闭弹框
    closeAssignDialog() {
      this.showRoleDialog = false
    }
  }
}
</script>

<style lang="less" scoped>
</style>
